package com.yc.jpyy.insurance.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.insurance.dao.InsuranceDao;
import com.yc.jpyy.insurance.service.InsuranceService;


@Service
public class InsuranceServiceImpl extends BaseService implements InsuranceService {
 

@Autowired
private InsuranceDao insuranceDao;

@Override
public Map<String, Object> addInsurance(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int count = insuranceDao.addInsurance(paramsMap);
	if(count <= 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "新增失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "新增成功");
	}
	return resultMap;
}

@Override
public Map<String, Object> selectInsuranceByPages(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	List<Map<String,Object>> professionList = insuranceDao.selectInsuranceByPages(paramsMap);
	int professionCounts = insuranceDao.selectInsuranceCount(paramsMap);
	if(professionList != null && professionList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",professionList);
		resultMap.put("TotalCount",professionCounts);
		resultMap.put("TotalPage",professionCounts % pagesize >0 ? professionCounts/pagesize + 1 : professionCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}

@Override
public Map<String, Object> delInsurance(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	paramsMap.put("ids", paramsMap.get("ids").toString().split(","));
	int count = insuranceDao.delInsurance(paramsMap);
	if(count <= 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "删除失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "删除成功");
	}
	return resultMap;
}


@Override
public Map<String, Object> selectInsurance(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	Map<String,Object>  professionMap = insuranceDao.selectInsurance(paramsMap);
	if(professionMap != null && professionMap.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData", professionMap);
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "无数据");
	}
	return resultMap;
}

@Override
public Map<String, Object> updateInsurance(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int i = insuranceDao.updateInsurance(paramsMap);
	if(i > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "修改成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "修改失败");
	}
	return resultMap;
}

@Override
public Map<String, Object> updateVehicleInsurance(Map<String, Object> paramsMap)
		throws Exception {
	Map<String,Object> resultMap = new HashMap<>();
	//更新保险表
	int count = insuranceDao.updateInsuranceInfo(paramsMap);
	if(count <= 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "更新失败");
	}else{
		if(paramsMap.get("status").toString().equals("2")){
			//更新保险表
			int k = insuranceDao.updateCarRentalInfo(paramsMap);
			if(k > 0){
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "更新成功");
			}else{
				throw new Exception();
			}
		}else{
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "更新成功");
		}
	}
	return resultMap;
}

}
